importScripts('./Cartesian3.js')
importScripts('./st.js')
function txtCurve2TimeMap(txt) {
    var rows = txt.split('\r\n')
    rows.pop()
    var time = ''
    var times = []
    var oneTime = new Map()
    rows.forEach(item => {
        if (item[4] === '-') { // 时间行
            if (oneTime.size > 0) times.push({ time: time, timeData: oneTime })
            time = item
            oneTime = new Map()
            return
        }
        var rowNumber = item.split(' ')
        var index = Number(rowNumber[0])
        var sw = Number(rowNumber[1])
        if (index && sw) {
            // oneTime.push(item)
            oneTime.set(index, sw)
        } else {
            console.log('横截面信息错误', rowNumber)
            return
        }
    })
    times.push({ time: time, timeData: oneTime })
    return times
}

// 等同于 this.addEventListener / addEventListener
this.addEventListener('message', function(res) {
    const { resRiverMeshs, resCurve, resPoint, timesNodes } = res.data
    var timesCurves = txtCurve2TimeMap(resCurve) //
    var resArr = []
    timesCurves.forEach((timeCurve, timeIndex) => {
        var meshsToSave = []
        resRiverMeshs.forEach(mesh => {
            // console.log(mesh[2])
            var oneMeshlonlat = []
            mesh[2].forEach(piStr => {
                var pi = Number(piStr)
                var lon = resPoint.lonlats[pi][0]
                var lat = resPoint.lonlats[pi][1]
                var h = 1000
                var hFormNode = timesNodes[timeIndex].timeData.get(pi)
                var hFormCurve = timeCurve.timeData.get(pi)
                if (!hFormNode) { // 如没从node.txt里拿到点高程
                    // 从一维截面取高度
                    h = hFormCurve
                } else {
                    // 如果 河道一维截面高度比外围底较多就以截面为准，用于处理退水时岸上积水问题
                    h = hFormNode - hFormCurve > 1 ? hFormCurve : hFormNode
                }
                oneMeshlonlat.push(lon)
                oneMeshlonlat.push(lat)
                oneMeshlonlat.push(h)
            })
            var angle = Number(mesh[1]) - 90
            angle = Math.round(angle / 15) * 15
            // eslint-disable-next-line use-isnan
            if (isNaN(angle)) {
                console.log(`获取角度错误:时间为${timeCurve.time},网格${mesh}`)
                return
            }
            var st = getST(oneMeshlonlat, angle, resPoint.maxLon, resPoint.minLon, resPoint.maxLat, resPoint.minLat)
            var cartesian3 = Cartesian3.fromDegreesArrayHeights(oneMeshlonlat)
            var cartesian3s = []
            cartesian3.map((item) => {
                cartesian3s.push(item.x)
                cartesian3s.push(item.y)
                cartesian3s.push(item.z)
            })
            var oneMeshToSave = {
                cartesian3s,
                st

            }
            meshsToSave.push(oneMeshToSave)
        })
        resArr.push({ time: timeCurve.time + '-In', meshs: meshsToSave })
    })

    this.postMessage(resArr)
    this.close()
}, false)
// 监听错误
this.addEventListener('error', function(e) {
    console.error(e)
}, false)
// this.close(); // 终止通信
